//
//  StatusCell.swift
//  我的微博
//
//  Created by Creator on 15/10/15.
//  Copyright © 2015年 Creator. All rights reserved.
//

import UIKit

 /// 微博cell的标示符枚举
enum StatusCellIdentifier: String {

    case NormalCell = "NormalCell"
    case ForwardCell = "ForwardCell"
    
    // 静态函数
    static func cellId(status: Status) -> String
    {
        return status.retweeted_status == nil ? StatusCellIdentifier.NormalCell.rawValue : StatusCellIdentifier.ForwardCell.rawValue
    }
}

/// 微博 cell
class StatusCell: UITableViewCell {

    ///  微博数据模型
    var status: Status? {
        didSet {
            topView.status = status
            contentLable.text = status?.text ?? ""
            
            // 设置配图视图数据
            pictureView.status = status!
            
            // 设置平日图视图的frame
            pictureHeighCons?.constant = pictureView.bounds.size.height
            pictureWidthCons?.constant = pictureView.bounds.size.width
            pictureTopCons?.constant = (pictureView.bounds.size.height == 0) ? 0 : 8


        }
    }
    
    ///  图片宽度约束
    var pictureWidthCons: NSLayoutConstraint?
    ///  图片宽度约束
    var pictureHeighCons: NSLayoutConstraint?
    ///  图片顶部约束
    var pictureTopCons: NSLayoutConstraint?
    
    ///  计算行高
    func rowHeight(status: Status) -> CGFloat {
    
        // 设置属性
        self.status = status
        // 强行更新布局
        layoutIfNeeded()
        // 返回底部视图最大高度
        return CGRectGetMaxY(bottomView.frame)
        
    }
    
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
//        backgroundColor = UIColor.redColor()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    ///  设置界面
     func setupUI() {
    
        // 添加控件
        contentView.addSubview(topView)
        contentView.addSubview(contentLable)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        
        // 自动布局
        // 顶部视图
         topView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 53))
        // 内容个标签
        contentLable.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: 8, y: 8))
        // 宽度
        contentView.addConstraint(NSLayoutConstraint(item: contentLable, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Width, multiplier: 1.0, constant: -16))
        
//        // 配图视图
//        let cons = pictureView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: contentLable, size: CGSize(width: 290, height: 290), offset: CGPoint(x: 0, y: 8.0))
//        pictureWidthCons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Width)
//        pictureHeighCons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Height)
//        pictureTopCons = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Top)
        
        // 底部约束
        bottomView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44), offset: CGPoint(x: -8, y: 8))
        
//        contentView.addConstraint(NSLayoutConstraint(item: bottomView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))
        

    }
    
    // MARK: - 懒加载控件
    private lazy var topView: StatusTopView = StatusTopView()
    
    ///  内容标签
     lazy var contentLable: UILabel = {
        let lable = UILabel(color: UIColor.darkGrayColor(), fontSize: 15)
       lable.numberOfLines = 0
        // 折行
        lable.preferredMaxLayoutWidth = UIScreen.mainScreen().bounds.width - 2 * 8
        return lable
    }()
    
     /// 配图视图
     lazy var pictureView: StatusPictureView = StatusPictureView()
    
    ///  底部视图
     lazy var bottomView: StatusBottomView = StatusBottomView()

}
